﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Common;
using DataTool;


namespace OneGateWF.ValidateTaskNS
{
    public sealed class CreateTask : CodeActivity
    {
        [RequiredArgument]
        public InArgument<ValidateTask> task { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            var inf=context.GetExtension<CreateAdditionalInfo>();
            task.Get(context).Create();
            task.Get(context).CreateLog(1, task.Get(context).NumCode, inf.Note);
        }
    }
    public sealed class SetTaskStatus : CodeActivity
    {
        [RequiredArgument]
        public InArgument<ValidateTask> task { get; set; }
        [RequiredArgument]
        public InArgument<byte> Status { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            task.Get(context).UpdateStatus(Status.Get(context));
        }
    }
    public sealed class Wait4Check : NativeActivity<ValidateSession>
    {
        [RequiredArgument]
        public InArgument<ValidateTask> task { get; set; }
        protected override void  Execute(NativeActivityContext context)
        {
            context.CreateBookmark("a", new BookmarkCallback(OnReadComplete));
        }

        void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
        {
            if (task.Get(context).GetType().Equals(Type.GetType(typeof(SimpleTask).AssemblyQualifiedName)))
            {
                ValidateSession va = (ValidateSession)state;
                context.SetValue(Result, va);
                byte LogKind = 0;
                if (va.IsAgree)
                    LogKind = 2;
                else
                    LogKind = 4;
                ((SimpleTask)task.Get(context)).CreateLog(LogKind, va.UserName, va.Note);
            }
            else if (task.Get(context).GetType().Equals(Type.GetType(typeof(ExcemptTuitionTask).AssemblyQualifiedName)))
            {
                ExcemptTuitionVS va = (ExcemptTuitionVS)state;
                context.SetValue(Result, va);
                ((ExcemptTuitionTask)task.Get(context)).Tuition = va.Tuition;
                ((ExcemptTuitionTask)task.Get(context)).Fault = va.Fault;
                ((ExcemptTuitionTask)task.Get(context)).SaveChange();
                byte LogKind = 0;
                if (va.IsAgree)
                    LogKind = 2;
                else
                    LogKind = 4;
                ((ExcemptTuitionTask)task.Get(context)).CreateLog(LogKind, va.UserName, va.Note);
            }

        }

        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }
    }

    public sealed class Notify : CodeActivity
    {
        [RequiredArgument]
        public InArgument<ValidateTask> task { get; set; }
        public InArgument<string> Note{get;set;}
        protected override void Execute(CodeActivityContext context)
        {
            var Ext = task.Get(context);
            TaskNotifyClass n = new TaskNotifyClass
            {
                 Cont = Note.Get(context),
                 TaskKind = Ext.TaskKind,
                 Username = Ext.NumCode,                 
            };
            n.NewNotify();
        }
    }

    public sealed class Wait4Approve : NativeActivity<ValidateSession>
    {
        [RequiredArgument]
        public InArgument<ValidateTask> task { get; set; }
        protected override void Execute(NativeActivityContext context)
        {
            context.CreateBookmark("b", new BookmarkCallback(OnReadComplete));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="bookmark"></param>
        /// <param name="state"></param>
        void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
        {

            ValidateSession va = (ValidateSession)state;
            context.SetValue(Result, va);
            byte LogKind = 0;
            if (va.IsAgree)
                LogKind = 3;
            else
                LogKind = 5;
            task.Get(context).CreateLog(LogKind, va.UserName, va.Note);
        }

        /// <summary>
        /// 
        /// </summary>
        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }
    }

    public sealed class Wait4MarkDone : NativeActivity
    {
        protected override void Execute(NativeActivityContext context)
        {
            context.CreateBookmark("c", new BookmarkCallback(OnReadComplete));
        }

        void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
        {
        }

        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }
    }

    public sealed class CancelHandler : CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            wfdbEntities db = new wfdbEntities();
            db.TASKs.DeleteObject(db.TASKs.SingleOrDefault(x => x.Id == context.WorkflowInstanceId));
            db.SaveChanges();
        }
    }
}
